/**
 * Created by Administrator on 2016/11/7.
 */
(function(){
    var companyValidate={
        init:function(){
            this.company_invite_insert();
        },
        company_invite_insert:function(){		/*新增立即询价*/
        	if($("#company_invite_insert").size() > 0){
        		$('form#company_invite_insert').submit(function(e) {

                    $("form#company_invite_insert input").removeClass("active");
        			if($("form#company_invite_insert input[name=product_name]").val()== ""){
        				$("form#company_invite_insert input[name=product_name]").addClass("active");
            
            			layer.msg("产品名称 必填", {icon:2,time:1000});
    			        return false;
            		}
        			var num = /^(?!0+(?:\.0+)?$)(?:[1-9]\d*|0)(?:\.\d{1,2})?$/;
            		if(!num.test($("input[name=one_num]").val())){ 
            			$("input[name=one_num]").addClass("active");
    			    	layer.msg("产品数量 请输入数字", {icon:2,time:1000});
    			        return false;
    			    }
            		if($("input[name=link_man]").val().length == 0){
            			$("input[name=link_man]").addClass("active");
            			layer.msg("联系人 必填", {icon:2,time:1000});
    			        return false;
            		}
            		if(!/^1\d{10}$/.test($("input[name=link_phone]").val())){
            			layer.msg("联系方式 必填", {icon:2,time:1000});
            			$("input[name=link_phone]").val('').addClass("active");
    			        return false;
            		}
            		$("#company_invite_insert input.active").removeClass("active");
        			$.ajax({
    			        type:"POST",
    			        dataType:"json",
    			        async: false,
                        url: get_baseUrl()+"Ccompany_invite/addPost",
                        data: $("form#company_invite_insert").serialize(),
                        beforeSend: function() {
                            $("#company_invite_insert input[type=submit]").val("正在提交...").attr("disabled", true);
                        },
    			        success: function(json) {
                            if (json.status) {
                            	layer.msg("操作成功,在刷新中", {icon:1,time:1000});
                                window.location.href = json.data;
                            } else {
                                $("#company_invite_insert input[type=submit]").val("提交").removeAttr("disabled");
                                layer.msg(json.errmsg, {icon:2,time:1000}, function(){
                                	if(json.data){window.location.href = json.data;}
                                });
                            }
                        }
    			    });
        			
                    e.preventDefault();
                });
        	}
        },
    };

    /*
     * 上传图片
     * */
    var upLoadImage={
        init:function(){
            this.str="";
            this.btn=$("#file-up1");
            this.content=$(".xj-add-pic ul");
            this.addPic();
            this.deletePic();
        },
        addPic:function(){
            var _this=this;
            this.btn.on("change",function(){
                var file = this.files[0];
                if (file.size > 5120 * 1024) {
                    layer.msg('图片不能超过5M', {icon: 2, time: 1000});
                    return;
                } else if (_this.content.children().length + 1 > 10) {
                	layer.msg("图片只能上传10张", {icon: 2, time: 1000});
                    return;
                }
                var src = window.URL.createObjectURL(file);
                $.ajaxFileUpload({
                    url: get_baseUrl()+'Cinvite/file_add', //你处理上传文件的服务端
                    secureuri: false,
                    fileElementId:"file-up1",
                    data: {},
                    dataType: 'json',
                    success: function (data) {
                        if (data) {
                            str = '<li><img src="'+src+'" alt=""><span class="xj-delete2" data-fileid="'+data+'"></span></li>';
                            _this.content.prepend(str);
                            if(_this.content.children().length>0){
                                $(".add-send-btn span").text("继续上传");
                            }
                        } else {
                            layer.msg('上传失败', {icon: 2, time: 1000});
                        }
                    }
                })
            });
        },
        deletePic:function(){
            $(".xj-add-pic").on("click",".xj-delete2",function(){
                var this_id = $(this);
                $.post(get_baseUrl() + 'Cinvite/file_delete', {file: this_id.data('fileid')}, function (json) {
                    if(json) {
                        this_id.parent('li').remove();
                        if(this_id.parents('ul').find('li').length==0) {
                            $(".add-send-btn span").text("上传附件");
                        }
                    }
                }, 'json');

            });
        }
    };

    companyValidate.init();
    upLoadImage.init();
})();